//
//  ContentView.swift
//  WebSocketDemo
//
//  Created by 姬向阳 on 2020/5/6.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @State var textFieldString: String = ""
    @State var showAlert: Bool = false
    @ObservedObject var chatManager = ChatManager()
    @ObservedObject private var keyboardResponder = KeyboardResponder()
    var body: some View {
        VStack {
            HStack {
                CustomTextField(limit: 6,placeholder: "昵称未设置", text: $chatManager.avatar)
                    .frame(maxWidth: 144 ,maxHeight: 22)
                Button(action: {
                    if self.chatManager.avatar.count > 0 {
                        UIApplication.shared.endEditing()
                        self.chatManager.connectServer()
                    } else {
                        self.showAlert = true
                    }
                }) {
                    Text("Connect")
                        .foregroundColor(.primary)
                        .customBorder()
                }
                .alert(isPresented: $showAlert) { () -> Alert in
                    Alert(title: Text("昵称未设置"), message: Text("请先设置昵称再点击连接服务器"), dismissButton: .default(Text("OK")))
                }
                Button(action: {
                    self.chatManager.disConnect()
                }) {
                    Text("Disconnect")
                        .foregroundColor(.primary)
                        .customBorder()
                }
            }
            List {
                ForEach(self.chatManager.messages, id: \.self) { message in
                    ChatCell(message: message)
                }
            }
            .onTapGesture {
                UIApplication.shared.endEditing()
            }
            HStack {
                CustomTextField(text: $textFieldString) {
                    if self.textFieldString.count > 0 {
                        self.chatManager.sendMessage(self.textFieldString)
                        self.textFieldString = ""
                    }
                }
                .frame(width: UIScreen.main.bounds.width - 120 ,height: 22)
                Button(action: {
                    if self.textFieldString.count > 0 {
                        self.chatManager.sendMessage(self.textFieldString)
                        self.textFieldString = ""
                    }
                }) {
                    Text("Send")
                        .foregroundColor(.primary)
                        .customBorder()
                }
            }
            .padding()
        }
        .padding(.bottom, keyboardResponder.keyboardHeight)
        .edgesIgnoringSafeArea(keyboardResponder.keyboardShow ? .bottom : [])
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

// MARK:- extension

extension View {
    // Customed RoundedRectangel Border of View
    func customBorder() -> some View {
        self.padding(.horizontal)
            .padding(.vertical, 6)
        .overlay(
            RoundedRectangle(cornerRadius: 4)
                .stroke(Color.primary)
        )
    }
}

